package light.head.dao.oa;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import light.head.cache.Cache;
import light.head.constant.Parm;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.framework.utils.DateUtils;
import light.head.model.VehicleAssign;

import org.springframework.stereotype.Service;


@Service
public class VehicleAssignDAO extends BaseDAO<VehicleAssign, Integer>{
	
	private final static String sql_loadAll = "FROM VehicleAssign";
	private final static String sql_findByPlate = "FROM VehicleAssign a WHERE a.oc = ? AND a.plate = ?)";
	private final static String sql_findPlate = "FROM VehicleAssign a WHERE a.ekm is null AND a.plate = ?)";
	
	
	public RESULT findByMap(List<Integer> ocs,Map<String, String> map , PageBean pb){
		if (ocs == null || ocs.size() == 0) return null;
		StringBuffer hql = new StringBuffer("FROM VehicleAssign a WHERE a.oc IN (");
		List<Object> list = new ArrayList<Object>();
	
		for (Integer i : ocs) {
			hql.append("?,");
			list.add(i);
		}
		hql.deleteCharAt(hql.length() - 1).append(")");
		if(map != null){
			String deleted = map.get(Parm.DELETED);
			String at = map.get(Parm.AT);
			String un = map.get(Parm.UN);
			String plate  = map.get(Parm.PLATE);
			if (isNotNull(deleted)) {
				list.add(Boolean.parseBoolean(deleted));
				hql.append(" AND a.deleted = ?");
			}
			if (isNotNull(un)) {
				list.add(un);
				hql.append(" AND a.un LIKE ?");
			}
			if (isNotNull(plate)) {
				list.add(plate);
				hql.append(" AND a.plate LIKE ?");
			}
			if (isNotNull(at)) {
				list.add(DateUtils.getDateByStr(at, null));
				hql.append(" AND a.at >= ?");
			}
		}	 
		hql.append(" ORDER BY a.code DESC");
		return findToPageBean(hql.toString(), pb, list);
	  }
	
	
	
	public RESULT findByMap1(String name,List<Integer> ocs,Map<String, String> map , PageBean pb){
		if (ocs == null || ocs.size() == 0) return null;
		StringBuffer hql = new StringBuffer("FROM VehicleAssign a WHERE a.un LIKE ? AND a.oc IN (");
		List<Object> list = new ArrayList<Object>();
		
		list.add(name);
		for (Integer i : ocs) {
			hql.append("?,");
			list.add(i);
		}
		hql.deleteCharAt(hql.length() - 1).append(")");
		if(map != null){
			String deleted = map.get(Parm.DELETED);
			String at = map.get(Parm.AT);
			String un = map.get(Parm.UN);
			String plate  = map.get(Parm.PLATE);
			
			if (isNotNull(deleted)) {
				list.add(Boolean.parseBoolean(deleted));
				hql.append(" AND a.deleted = ?");
			}
			if (isNotNull(un)) {
				list.add(un);
				hql.append(" AND a.un LIKE ?");
			}
			if (isNotNull(plate)) {
				list.add(plate);
				hql.append(" AND a.plate LIKE ?");
			}
			if (isNotNull(at)) {
				list.add(DateUtils.getDateByStr(at, null));
				hql.append(" AND a.at >= ?");
			}
		}	 
		hql.append(" ORDER BY a.code DESC");
		return findToPageBean(hql.toString(), pb, list);
	  }
	 
	
	
	
	public RESULT update(VehicleAssign va) {
			if (null == va || null == va.getCode())
				return RESULT.PARAMETER_ERROR;
			
			VehicleAssign old = get(va.getCode());
			if (null == old)
				return RESULT.NOT_FIND;
			if (RESULT.SUCCESS == upd(old)) {
				this.loadAll();
				return RESULT.SUCCESS;
			} else {
				return RESULT.ERROR;
			}
		}
	 
	public List<VehicleAssign> getAll() {
		if (null == Cache.vehicleassign || Cache.vehicleassign.size() == 0)
			return loadAll();
		return Cache.vehicleassign;
	}
			
	public List<VehicleAssign> loadAll() {
		Cache.vehicleassign = find(sql_loadAll);
		return Cache.vehicleassign;
	}

	
	public List<VehicleAssign> findByPlate(Integer oc, String plate) {
		List<VehicleAssign> list = find(sql_findByPlate, oc, plate);
		return list;
	}
	
	public VehicleAssign findPlate(String plate) {
		List<VehicleAssign> list = find(sql_findPlate, plate);
		return list.get(0);
	}
}
